//
//  LGCiTangCreateBiographyController.m
//  ligenyun
//
//  Created by Mac on 2018/3/26.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "LGCiTangCreateBiographyController.h"

@interface LGCiTangCreateBiographyController ()
@property(nonatomic, strong) NSArray                     *dataSouce;
@end

@implementation LGCiTangCreateBiographyController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"创建传记";
    [self configBottomView];
    [self configDataSouce];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configBottomView{
    __weak typeof(self) weakSelf =  self;
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth / 2, 49)
                                                     title:@"提交"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:18
                                              cornerRadius:0
                                           backgroundColor:TM_redColor
                                           backgroundImage:nil
                                                     image:nil];
    [buyButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedCommitButton];
    }];
    buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    buyButton.top = [TMUtils tabBarTop];
    [self.view addSubview:buyButton];
    
    STButton *resetButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth / 2, 49)
                                                      title:@"重置"
                                                 titleColor:FirstTextColor
                                                  titleFont:18
                                               cornerRadius:0
                                            backgroundColor:FlatGray
                                            backgroundImage:nil
                                                      image:nil];
    [resetButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedResetButton];
    }];
    resetButton.left = UIScreenWidth /2 ;
    [self.view addSubview:resetButton];
    resetButton.top = [TMUtils tabBarTop];
    resetButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}
- (void)configDataSouce{
    
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:@"传记:" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"谱系本记:" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"祖产:" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"契据文约:" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"坟茔:" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"名迹录:" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"任宦记第:" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"年谱:" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"字辈谱:" detailString:@""]
                       ];
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * model = self.dataSouce[indexPath.section];
    [cell setSt_tableEasyModel:model];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.section];

    NSString * placeHolder = [NSString stringWithFormat:@"请输入%@",model.textString];
    STTableViewDetailController * detail =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                           title:model.textString
                                                            text:model.detailString
                                                          handle:^(NSString *text) {
                                                              if (text.length && ![text isEqualToString:model.detailString]) {
                                                                  model.detailString = text;
                                                                  [self.tableView reloadData];
                                                              }
                                                              if (!text.length) {
                                                                  
                                                                  [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@不能为空",model.textString]];
                                                              }
                                                              [self.navigationController popViewControllerAnimated:YES];
                                                          }];
    detail.detailLabel.text = @"";
    detail.isShowTextNum = NO;
    [self.navigationController pushViewController:detail animated:YES];
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedCommitButton{
    for (STTableEasyModel * mdoel in self.dataSouce) {
        if (!mdoel.detailString.length) {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@不能为空",mdoel.textString]];
            return;
        }
    }

    [TMUtils debugSimulationNetWorkWithHadle:^(NSInteger afterTime) {
        [SVProgressHUD showSuccessWithStatus:@"提交成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
- (void)onSelctedResetButton{
    [self showAlertCancelAndConfirm:@"重置后,填写的资料会消失，确认重置吗？" andWithBlock:^(NSString *name) {
        if ([name isEqualToString:@"确认"]) {
            [self configDataSouce];
        }
    }];
    
}
@end
